export default {
  methods: {
    mergeMenu(firstMenu, subMenu) {
      // merge submenus into corresponding parent menus
      for (let i = 0; i < subMenu.length; ++i) {
        const { parentid } = subMenu[i];
        for (let j = 0; j < firstMenu.length; j++) {
          if (!firstMenu[j].children) firstMenu[j].children = [];
          if (firstMenu[j].menuid === parentid)
            firstMenu[j].children.push(subMenu[i]);
        }
      }
      return firstMenu;
    },
    filterMenu(menus, val) {
      if (val === "") return menus;
      const res = [];
      for (let i = 0; i < menus.length; i++) {
        const menu = menus[i];
        if (menu.menuname.includes(val)) {
          res.push(menu);
          continue;
        }
        if (menu.children.length)
          for (let j = 0; j < menu.children.length; j++) {
            const subMenu = menu.children[j];
            subMenu.menuname.includes(val) && res.push(subMenu);
          }
      }
      return res;
    }
  }
};
